package com.jit.monitor.ui.activity.user;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.util.ui.StatusBarHelper;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.monitor.R;
import com.jit.monitor.presenter.user.RestPwdAtPresenter;
import com.jit.monitor.view.RestPwdAtView;

import butterknife.BindView;

/**
 * @author crazyZhangxl on 2018-12-11 14:35:12.
 * Describe:
 */

public class RestPwdActivity extends BaseActivity<RestPwdAtView,RestPwdAtPresenter> implements RestPwdAtView {

    @BindView(R.id.ivNewSeeFirst)
    ImageView mIvNewSeeFirst;
    @BindView(R.id.ivNewDelFirst)
    ImageView mIvNewDelFirst;
    @BindView(R.id.etNewPwdFirst)
    EditText mEtNewPwdFirst;
    @BindView(R.id.ivNewSeeSecond)
    ImageView mIvNewSeeSecond;
    @BindView(R.id.ivNewDelSecond)
    ImageView mIvNewDelSecond;
    @BindView(R.id.etNewPwdSecond)
    EditText mEtNewPwdSecond;
    @BindView(R.id.btnChangePwd)
    Button mBtnChangePwd;
    @Override
    protected void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_rest_pwd;
    }

    @Override
    protected RestPwdAtPresenter createPresenter() {
        return new RestPwdAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarHelper.setStatusBarLightMode(this);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mEtNewPwdFirst.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean isAccountNull = mEtNewPwdFirst.getText().toString().trim().length() != 0;
                boolean isPwdNull = mEtNewPwdSecond.getText().toString().trim().length() != 0;
                mBtnChangePwd.setEnabled(isAccountNull && isPwdNull);
                if (isAccountNull){
                    mIvNewDelFirst.setVisibility(View.VISIBLE);
                }else {
                    mIvNewDelFirst.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtNewPwdFirst.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                if (mEtNewPwdFirst.getText().toString().trim().length() != 0){
                    mIvNewDelFirst.setVisibility(View.VISIBLE);
                }
            }else {
                mIvNewDelFirst.setVisibility(View.GONE);
            }
        });

        mEtNewPwdSecond.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                if (mEtNewPwdSecond.getText().toString().trim().length() != 0){
                    mIvNewDelSecond.setVisibility(View.VISIBLE);
                }else {
                    mIvNewDelSecond.setVisibility(View.GONE);
                }
            }else {
                mIvNewDelSecond.setVisibility(View.GONE);
            }
        });
        mEtNewPwdSecond.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                boolean isAccountNull = mEtNewPwdFirst.getText().toString().trim().length() != 0;
                boolean isPwdNull = mEtNewPwdSecond.getText().toString().trim().length() != 0;
                mBtnChangePwd.setEnabled(isAccountNull && isPwdNull);
                if (isPwdNull){
                    mIvNewDelSecond.setVisibility(View.VISIBLE);
                }else {
                    mIvNewDelSecond.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mIvNewDelFirst.setOnClickListener(v -> {
            mEtNewPwdFirst.setText("");
            mBtnChangePwd.setEnabled(false);
        });

        mIvNewDelSecond.setOnClickListener(v -> {
            mEtNewPwdSecond.setText("");
            mBtnChangePwd.setEnabled(false);
        });

        // 密码可见的状态改变事件
        mIvNewSeeFirst.setOnClickListener(v -> {
            if (mEtNewPwdFirst.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
                // 由可见变为不可见
                mEtNewPwdFirst.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mIvNewSeeFirst.setImageResource(R.mipmap.ic_see_can_not);
            } else {
                // 由不可见变为可见
                mEtNewPwdFirst.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mIvNewSeeFirst.setImageResource(R.mipmap.ic_see_can);
            }
            /* 光标移动到的位置*/
            mEtNewPwdFirst.setSelection(mEtNewPwdFirst.getText().toString().trim().length());
        });

        mIvNewSeeSecond.setOnClickListener(v -> {
            if (mEtNewPwdSecond.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
                mEtNewPwdSecond.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mIvNewSeeSecond.setImageResource(R.mipmap.ic_see_can_not);
            } else {
                mEtNewPwdSecond.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mIvNewSeeSecond.setImageResource(R.mipmap.ic_see_can);
            }
            /* 光标移动到的位置*/
            mEtNewPwdSecond.setSelection(mEtNewPwdSecond.getText().toString().trim().length());
        });

        mBtnChangePwd.setOnClickListener(v -> {
            String pwdFirst = mEtNewPwdFirst.getText().toString();
            String pwdSecond = mEtNewPwdSecond.getText().toString();
            if (pwdFirst.equals(pwdSecond)){
                mPresenter.restPwd(pwdFirst);
            }else {
                UIUtils.showToast("密码不一致");
                mBtnChangePwd.setEnabled(false);
            }
        });
    }

    @Override
    public void restPwdSuccess() {
        Toast.makeText(mContext, "重置密码成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void restPwdFailure(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
}
